﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using InkFx.WinControl.Core;
using DA = Temp_20190707_AForm.DockAlignment;
using DTGS = Temp_20190707_AForm.DockTabGroupState;

namespace Temp_20190707_AForm
{
    public partial class DockFloatForm : System.Windows.Forms.Form
    {
        public DockFloatForm()
        {
            InitializeComponent();
            base.Padding = new Padding(PADDING);
            base.DoubleBuffered = true;
        }

        public static DockFloatForm ShowControl(DockPanel dockPanel, DockTabGroup group, Rectangle rect)
        {
            DockPopPanel.TogglePopGroup(null);

            DockFloatForm form = new DockFloatForm();
            form.TabGroup = group;
            form.DockPanel = dockPanel;

            if (rect != Rectangle.Empty)
            {
                form.StartPosition = FormStartPosition.Manual;
                form.Bounds = rect;
            }

            group.Dock = DockStyle.Fill;
            group.State = DTGS.Float;
            form.Controls.Add(group);
            form.Show(dockPanel.FindForm());
            return form;
        }

        public const int PADDING = 3;
        public DockTabGroup TabGroup { get; private set; }
        public DockPanel DockPanel { get; private set; }


        protected override void OnControlRemoved(ControlEventArgs e)
        {
            base.OnControlRemoved(e);

            DockTabGroup group = null;
            foreach (var ctrl in this.Controls)
                if (ctrl is DockTabGroup) { group = (DockTabGroup)ctrl; break; }

            if (group != null) { this.TabGroup = group; }
            else { Dispose(); }
        }
        public new void Dispose()
        {
            var group = this.TabGroup;
            var form = group == null ? null : group.FindForm();
            if (form == this) group.Parent = null;

            TabGroup = null;
            DockPanel = null;
            this.Close();
            base.Dispose();
        }
    }
}
